package com.gooluke.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.Getter;
import lombok.Setter;

/**
 * @author gooluke
 * description 代码生成器
 * datetime 2025-03-27 20:07
 */
@Getter
@Setter
public class MybatisPlusCodeGenerator {
    public static final String JDBC_URL_FORMAT = "jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC";

    private String userName = ""; // 数据库账号
    private String password = ""; // 数据库密码
    private String host = "localhost"; // 数据库主机地址
    private long port = 3306; // 数据库端口
    private String database = "short-url"; // 数据库名称
    private String projectLocation = ""; // 项目位置
    private String outputDir = System.getProperty("user.dir") + this.projectLocation + "/src/test/java"; // 输出目录
    private String parent = "com.gooluke"; // 父包名
    private String entity = "entity"; // 实体类包名
    private String mapper = "mapper"; // Mapper接口包名
    private String dao = "dao"; // DAO接口包名
    private String service = "service"; // Service接口包名
    private String controller = "controller"; // Controller接口包名
    private String author = "Undefined"; // 作者

    public static MybatisPlusCodeGeneratorBuilder builder() {
        return new MybatisPlusCodeGeneratorBuilder();
    }

    public void execute(String... tableNames) {

        if ("".equals(this.userName) || "".equals(this.password) || "".equals(this.database)) {
            System.err.println("请配置 数据库账号、密码、数据库名称");
            return;
        }

        // 1. 数据源配置
        DataSourceConfig dsc = new DataSourceConfig.Builder(String.format(JDBC_URL_FORMAT,
                host, port, this.database), this.userName, this.password).build();

        // 2. 全局配置
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .outputDir(this.outputDir)
                .author(this.author)
                .disableOpenDir()
                .build();

        // 3. 包配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent(this.parent)
                .entity(this.entity)
                .mapper(this.mapper)
                .service(this.service)
                .controller(this.controller)
                .serviceImpl(this.dao)
                .build();

        // 4. 策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .addInclude(tableNames) // 要生成的表名
                .entityBuilder()
                .naming(NamingStrategy.underline_to_camel) // 实体类命名规则，使用驼峰规则
                .columnNaming(NamingStrategy.underline_to_camel)//字段名称规则，使用驼峰规则
                .enableLombok()//使用lombok
                .enableTableFieldAnnotation()//字段和表注解
                .enableFileOverride()//覆盖文件
                .build();

        // 5. 执行生成
        AutoGenerator generator = new AutoGenerator(dsc);
        generator.global(globalConfig).packageInfo(packageConfig).strategy(strategyConfig);
        generator.execute();

        System.out.println("执行成功...");
    }

    public void setProjectLocation(String projectLocation) {
        this.projectLocation = projectLocation;
        this.outputDir = System.getProperty("user.dir") + this.projectLocation + "/src/test/java";
    }

}
